package java.hilyb.dbtest;

import android.app.Activity;
import android.content.*;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class MainActivity extends Activity {
    private GridView gridView;
    private Integer[] ids = new Integer[]{
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R.drawable.p10,
            R.drawable.p11, R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15, R.drawable.p16, R.drawable.p17, R.drawable.p18
    };

    //根据电话号码查询姓名（在一个电话打过来时，如果此电话在通讯录中，则显示姓名）
    public void testReadNameByPhone(){
        String phone = "12345678";
        //uri=  content://com.android.contacts/data/phones/filter/#
        Uri uri = Uri.parse("content://com.android.contacts/contacts");
        ContentResolver resolver =  this. getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME}, null, null, null); //从raw_contact表中返回display_name
        while(cursor.moveToNext()){
            Log.i("Contacts", "name=" + cursor.getString(0));
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView=(GridView)findViewById(R.id.gridView);
        gridView.setAdapter(new MyGridAdapter(this, ids));
        gridView.setNumColumns(4);
        gridView.setVerticalSpacing(0);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                view.setBackgroundColor(MainActivity.this.getResources().getColor(android.R.color.white));
            }
        });

        Intent intent=new Intent(this,ServiceDemo.class);
        startService(intent);
    }
}

class MyGridAdapter extends BaseAdapter {
    private Integer[] ids;
    private Context ctx;
    private LayoutInflater mInflater;
    public MyGridAdapter(Context ctx,Integer[] ids){
        this.ids=ids;
        this.ctx=ctx;
        mInflater=LayoutInflater.from(ctx);
    }
    @Override
    public int getCount() {
        return ids.length;
    }

    @Override
    public Object getItem(int i) {
        return ids[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View item = mInflater.inflate(R.layout.grid_cell, null);
       /* ImageView img = (ImageView)item.findViewById(R.id.image);
        img.setImageResource((Integer)getItem(i));*/
        return item;
    }
}
